1   /*
2    * Copyright (C) 2013 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.google;
18  
19  import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD;
20  import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
21  import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
22  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
23  import static com.google.common.truth.Truth.assertThat;
24  
25  import com.google.common.annotations.GwtCompatible;
26  import com.google.common.collect.testing.features.CollectionFeature;
27  import com.google.common.collect.testing.features.CollectionSize;
28  
29  import java.util.Collections;
30  import java.util.Set;
31  
32  /**
33   * Tests for {@code Multiset.elementSet()} not covered by the derived {@code SetTestSuiteBuilder}.
34   * 
35   * @author Louis Wasserman
36   */
37  @GwtCompatible
38  public class MultisetElementSetTester<E> extends AbstractMultisetTester<E> {
39    @CollectionFeature.Require(SUPPORTS_ADD)
40    public void testElementSetReflectsAddAbsent() {
41      Set<E> elementSet = getMultiset().elementSet();
42      assertFalse(elementSet.contains(samples.e3));
43      getMultiset().add(samples.e3, 4);
44      assertTrue(elementSet.contains(samples.e3));
45    }
46    
47    @CollectionSize.Require(absent = ZERO)
48    @CollectionFeature.Require(SUPPORTS_REMOVE)
49    public void testElementSetReflectsRemove() {
50      Set<E> elementSet = getMultiset().elementSet();
51      assertTrue(elementSet.contains(samples.e0));
52      getMultiset().removeAll(Collections.singleton(samples.e0));
53      assertFalse(elementSet.contains(samples.e0));
54    }
55  
56    @CollectionSize.Require(absent = ZERO)
57    @CollectionFeature.Require(SUPPORTS_REMOVE)
58    public void testElementSetRemovePropagatesToMultiset() {
59      Set<E> elementSet = getMultiset().elementSet();
60      assertTrue(elementSet.remove(samples.e0));
61      assertFalse(getMultiset().contains(samples.e0));
62    }
63  
64    @CollectionSize.Require(SEVERAL)
65    @CollectionFeature.Require(SUPPORTS_REMOVE)
66    public void testElementSetRemoveDuplicatePropagatesToMultiset() {
67      initThreeCopies();
68      Set<E> elementSet = getMultiset().elementSet();
69      assertTrue(elementSet.remove(samples.e0));
70      assertThat(getMultiset()).isEmpty();
71    }
72  
73    @CollectionFeature.Require(SUPPORTS_REMOVE)
74    public void testElementSetRemoveAbsent() {
75      Set<E> elementSet = getMultiset().elementSet();
76      assertFalse(elementSet.remove(samples.e3));
77      expectUnchanged();
78    }
79    
80    @CollectionFeature.Require(SUPPORTS_REMOVE)
81    public void testElementSetClear() {
82      getMultiset().elementSet().clear();
83      assertThat(getMultiset()).isEmpty();    
84    }
85  }